﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace DatabaseLayer
{
    /// <summary>
    /// Create a connection to the db
    /// </summary>
    public class ConnectionDB
    {
        private static SqlConnection conn;
        private static ConnectionDB instance;
        
        /// <summary>
        /// Private constructor, following singelton pattern
        /// </summary>
        private ConnectionDB() 
        {
            // MS SQL Server
            string dataSource = "Data Source=balder.ucn.dk;";
            string initialCatalog = "Initial Catalog=dmaa0212a_5;";
            string userId = "User Id=dmaa0212a_5;";
            string password = "Password=MaaGodt;";
            string connectionString = dataSource + initialCatalog + userId + password;
            conn = new SqlConnection(connectionString);

            try
            {
                conn.Open();
                Console.WriteLine("State: {0}", conn.State);
                Console.WriteLine("ConnectionString: {0}", conn.ConnectionString);
            }
            catch (SqlException e)
            {
                throw e;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// Get ConnectionDB instance
        /// </summary>
        public static ConnectionDB Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new ConnectionDB();
                }
                return instance;
            }
        }

        /// <summary>
        /// Get open db connection
        /// </summary>
        /// <returns>A open connection to the database</returns>
        public SqlConnection getDBCon()
        {
            return conn;
        }
    }
}
